package com.ylp.sys.controller;

import com.ylp.common.response.PageResult;
import com.ylp.common.response.Result;
import com.ylp.sys.domain.dto.RoleDto;
import com.ylp.sys.domain.dto.RolePageDto;
import com.ylp.sys.domain.entity.SysRole;
import com.ylp.sys.service.SysRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统角色表 前端控制器
 * </p>
 *
 * @author cc
 * @since 2024-07-24
 */
@Tag(name = "系统角色接口")
@RestController
@RequestMapping("/role")
public class SysRoleController {
    private final SysRoleService sysRoleService;

    public SysRoleController(SysRoleService sysRoleService) {
        this.sysRoleService = sysRoleService;
    }

    @Operation(summary = "查询角色选择列表")
    @GetMapping("/roleSelectList")
    public Result<List<Map<String, Object>>> selectList() {
        List<Map<String, Object>> list = sysRoleService.getSelectList();
        return Result.success(list);
    }

    @PostMapping("/pageList")
    @Operation(summary = "分页查询角色列表")
    public Result<PageResult<SysRole>> pageList(@RequestBody RolePageDto rolePageDto) {
        PageResult<SysRole> pageResult = sysRoleService.pageList(rolePageDto);
        return Result.success(pageResult);
    }

    @Operation(summary = "添加角色")
    @PostMapping("/add")
    public Result<String> add(@RequestBody RoleDto roleDto) {
        try {
            sysRoleService.addRole(roleDto);
        } catch (Exception e) {
            return Result.error("添加失败");
        }
        return Result.success();
    }

    @Operation(summary = "获取角色和菜单ids")
    @GetMapping("/getRoleAndMenuIdsById")
    public Result<RoleDto> getRoleAndMenuIdsById(@RequestParam Long id) {
        return Result.success(sysRoleService.getRoleAndMenuIdsById(id));
    }

    @Operation(summary = "修改角色信息")
    @PutMapping("/edit")
    public Result<String> edit(@RequestBody RoleDto roleDto) {
        try {
            sysRoleService.updateAndMenus(roleDto);
        } catch (Exception e) {
            return Result.error();
        }
        return Result.success();
    }

    @Operation(summary = "删除角色")
    @PostMapping("/delete")
    public Result<String> delete(@RequestBody List<Long> ids) {
        try {
            sysRoleService.deleteByIds(ids);
        } catch (Exception e) {
            return Result.error();
        }
        return Result.success();
    }

    @Operation(summary = "修改角色状态")
    @PutMapping("/updateStatus")
    public Result<String> updateStatus(@RequestBody SysRole role) {
        sysRoleService.updateStatus(role);
        return Result.success();
    }

    @Operation(summary = "查询权限分配和未分配菜单树")
    @GetMapping("/searchAuthAndNot")
    public Result<Map<String, Object>> searchAuthAndNot(@RequestParam("roleId") Long roleId) {
        return Result.success(sysRoleService.searchAuthAndNot(roleId));
    }

}
